package com.gdust.hzx.manager.controller;

import com.gdust.hzx.manager.service.ProductService;
import com.gdust.spzx.common.log.annotation.Log;
import com.gdust.spzx.common.log.enums.OperatorType;
import com.gdust.hzx.model.dto.product.ProductDto;
import com.gdust.hzx.model.entity.product.Product;
import com.gdust.hzx.model.vo.common.Result;
import com.gdust.hzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: ProductController
 * Package: com.gdust.spzx.manager.controller
 * Description:产品控制类
 *
 * @Author syun
 * @Create 2024/12/22 17:11
 * @Version 1.0
 */
@RestController// @RestController: 专用于restful风格的注解 = @Controller (控制器) + @ResponseBody (方法返回值为json)
@RequestMapping(value = "/admin/product/product")
public class ProductController {
    //注入产品表的service
    @Autowired
    private ProductService productService;


    /**
     * 分页查询产品
     *
     * @param page       分页数
     * @param limit      每页记录数
     * @param productDto 商品搜索条件
     * @return
     */
    @GetMapping(value = "/{page}/{limit}")
    public Result<PageInfo<Product>> findByPage(@PathVariable Integer page,
                                                @PathVariable Integer limit,
                                                ProductDto productDto) {
        //自定义方法：分页查询产品
        PageInfo<Product> pageInfo = productService.findByPage(page, limit, productDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 保存商品数据
     *
     * @param product 商品实体
     * @return
     */
//    @Log(title = "保存商品",businessType = 1,operatorType = OperatorType.MANAGE)
    @PostMapping("/save")
    public Result save(@RequestBody Product product) {
        //自定义方法：保存商品数据
        productService.save(product);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询商品详情
     *
     * @param id 商品id
     * @return
     */
    @GetMapping("/getById/{id}")
    public Result<Product> getById(@PathVariable Long id) {
        //自定义查询：查询商品详情
        Product product = productService.getById(id);
        return Result.build(product, ResultCodeEnum.SUCCESS);
    }

    /**
     * 保存修改数据接口
     *
     * @param product
     * @return
     */
//    @Log(title = "保存修改数据",businessType = 1,operatorType = OperatorType.MANAGE)
    @PutMapping("/updateById")
    public Result update(@RequestBody Product product) {
        //自定义方法：保存修改数据
        productService.update(product);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据传入id删除商品
     *
     * @param id
     * @return
     */
    @Log(title = "保存修改数据",businessType = 3,operatorType = OperatorType.MANAGE)
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@Parameter(
            name = "id",
            description = "商品id",
            required = true) @PathVariable Long id) {
        //自定义方法：根据id删除商品
        productService.deleteById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 商品审核接口
     * @param id
     * @param auditStatus
     * @return
     */
    @Log(title = "商品审核",businessType = 0,operatorType = OperatorType.MANAGE)
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public Result updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
        productService.updateAuditStatus(id, auditStatus);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 商品上下架接口
     * @param id
     * @param status
     * @return
     */
    @Log(title = "商品上下架",businessType = 0,operatorType = OperatorType.MANAGE)
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        productService.updateStatus(id, status);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


}
